import 'package:casdon/modules/recipe/data/api/recipe_ecprotocol_api.dart';
import 'package:tuya_home_sdk_flutter/tuya_home_sdk_flutter.dart';

abstract class RecipeEcoprotocol {
  List<Map<String, String>> get cookParams;
  Map<String, dynamic> get dps;
}

class RecipeEcoprotocolModel extends RecipeEcoprotocol {
  @override
  late final List<Map<String, String>> cookParams;
  @override
  late final Map<String, dynamic> dps;

  // 静态方法转换
  static RecipeEcoprotocolModel? fromApi(
    ResponseRecipeEcprotocolApi? response,
    ThingSmartDeviceModel devModel,
  ) {
    if (response == null) return null;
    final stepInfos = response.protocolStepInfoVos;
    if (stepInfos == null) return null;
    if (response.electronicControlProtocol == null) return null;
    List<Map<String, String>> cookParamList = [];
    Map<String, String> cookParams = {};
    String protocol = response.electronicControlProtocol!;

    for (var step in stepInfos) {
      if (step.modeName != null) {
        cookParams.addAll({"模式": '${step.modeName}'});
      }

      if (step.argPrimaryName != null && step.argPrimary != null) {
        if (step.argPrimaryName!.contains("温度")) {
          cookParams.addAll({step.argPrimaryName!: '${step.argPrimary!}℃'});
        } else if (step.argPrimaryName!.contains("功率")) {
          cookParams.addAll({step.argPrimaryName!: '${step.argPrimary!}W'});
        } else {
          cookParams.addAll({step.argPrimaryName!: '${step.argPrimary}'});
        }
      }

      if (step.argSecondaryName != null && step.argSecondary != null) {
        if (step.argSecondaryName!.contains("温度")) {
          cookParams.addAll({step.argSecondaryName!: '${step.argSecondary!}℃'});
        } else if (step.argPrimaryName!.contains("功率")) {
          cookParams.addAll({step.argSecondaryName!: '${step.argSecondary!}W'});
        } else {
          cookParams.addAll({step.argSecondaryName!: '${step.argSecondary}'});
        }
      }

      var stepTime = "";
      if (step.timeHour != null && step.timeHour != 0) {
        stepTime = '${step.timeHour}时';
      }
      if (step.timeMin != null) {
        stepTime = '$stepTime${step.timeMin}分';
      }
      if (step.timeSec != null) {
        stepTime = '$stepTime${step.timeSec}秒';
      }
      if (stepTime.isNotEmpty) {
        cookParams.addAll({"时间": stepTime});
      }
    }
    cookParamList.add(cookParams);

    String allModeKey =
        '${getDpidWithDpCode(dpCode: "all_mode", devModel: devModel)}';

    Map<String, dynamic> dps = {
      allModeKey: "36",
      '${getDpidWithDpCode(dpCode: "diy_mode", devModel: devModel)}':
          response.electronicControlProtocol,
      '${getDpidWithDpCode(dpCode: "cloud_menu_id", devModel: devModel)}':
          response.recipeId,
      '${getDpidWithDpCode(dpCode: "start", devModel: devModel)}': true,
      '${getDpidWithDpCode(dpCode: "switch", devModel: devModel)}': true,
    };
    if (getDpTypeWithDpCode(dpCode: "all_mode", devModel: devModel) ==
        "value") {
      dps = {
        allModeKey: 36,
        '${getDpidWithDpCode(dpCode: "diy_mode", devModel: devModel)}':
            response.electronicControlProtocol,
        '${getDpidWithDpCode(dpCode: "cloud_menu_id", devModel: devModel)}':
            response.recipeId,
        '${getDpidWithDpCode(dpCode: "start", devModel: devModel)}': true,
        '${getDpidWithDpCode(dpCode: "switch", devModel: devModel)}': true,
      };
    }
    RecipeEcoprotocolModel model = RecipeEcoprotocolModel();
    model.dps = dps;
    model.cookParams = cookParamList;
    return model;
  }

  static int getDpidWithDpCode({
    required String dpCode,
    required ThingSmartDeviceModel devModel,
  }) {
    for (var schemaModel in devModel.schemaArr!) {
      if (dpCode == schemaModel.code) {
        return schemaModel.id;
      }
    }
    return 0;
  }

  static String? getDpTypeWithDpCode({
    required String dpCode,
    required ThingSmartDeviceModel devModel,
  }) {
    if (devModel.schemaArr == null) return null;
    for (var schemaModel in devModel.schemaArr!) {
      if (dpCode == schemaModel.code) {
        return propertyTypeValues.reverse[schemaModel.property!.type];
      }
    }
    return null;
  }
}
